
import json
import collections
import os, re

def load(file):
    with open(file,'r',encoding='utf-8-sig') as f:
        data = json.load(f)
        return data

def get_paths(source):
    paths = []
    if isinstance(source, collections.MutableMapping):  # found a dict-like structure...
        for k, v in source.items():  # iterate over it; Python 2.x: source.iteritems()
            paths.append([k])  # add the current child path
            paths += [[k] + x for x in get_paths(v)]  # get sub-paths, extend with the current
    # else, check if a list-like structure, remove if you don't want list paths included
    elif isinstance(source, collections.Sequence) and not isinstance(source, str):
        # Python 2.x: use basestring instead of str ^
        for i, v in enumerate(source):
            paths.append([i])
            paths += [[i] + x for x in get_paths(v)]  # get sub-paths, extend with the current
    return paths

def dump_path(j):
    fo = open("e:\\path_output.txt", 'w' ,encoding='utf8')
    ps = get_paths(j)
    for p in ps:
        fo.write("/".join(str(i) for i in p[1:])+"\n")
    fo.close()

def main():
    pass
    fo = open("e:\\wikionary(en only)20200107.csv", 'w' ,encoding='utf8')

    j=load("E:\w\\wikionary(en only)20200107.json")
    
    #return dump_path(j)
    pos_m = {
        "abbr."                         :"",
        "abbreviation"                  :"",
        "adj."                          : "adj.",
        "adjective"                     : "adj.",
        "adv."                          : "adv.",
        "adverb"                        : "adv.",
        "art."                          :"",
        "article"                       : "",
        "aux."                          :"aux.v.",
        "auxiliary verb"                : "aux.v.",
        "cardinal number"               :"",
        "comb."                         : "",
        "conj."                         :"",
        "conjunction"                   : "",
        "contr."                        : "",
        "contraction"                   : "",
        "det."                          :"",
        "determiner & ordinal number"   :"",
        "determiner"                    : "",
        "exclamation"                   : "",
        "form combining"                :"",
        "imperative verb"               : "vi.",
        "infinitive marker"             :"",
        "infix"                         : "",
        "initialism"                    : "",
        "int."                          :"",
        "interjection"                  : "",
        "interrogative adverb"          : "adv.",
        "interrogative pronoun"         : "",
        "intransitive verb"             : "vi.",
        "letter"                        : "",
        "link verb"                     :"link.v.",
        "modal verb"                    : "modal.v.",
        "n."                            : "n.",
        "noun"                          : "n.",
        "num."                          :"",
        "number"                        : "",
        "numeral"                       : "",
        "ord."                          :"",
        "ordinal number"                :"",
        "participle"                    : "",
        "particle"                      : "",
        "phrase"                        : "",
        "plural noun"                   : "n.",
        "possessive determiner"         : "",
        "possessive pronoun"            : "",
        "predeterminer"                 : "",
        "pref."                         :"",
        "prefix"                        :"",
        "prep."                         :"",
        "preposition"                   : "",
        "prepositional phrase"          : "",
        "pron."                         :"",
        "pronoun"                       : "",
        "proper noun"                   : "",
        "quantitative"                  : "",
        "relative adverb"               : "adv.",
        "relative determiner"           : "",
        "relative possessive determiner": "",
        "relative pronoun"              : "",
        "short."                        :"",
        "suff."                         :"",
        "suffix"                        :"",
        "sym."                          :"",
        "symb."                         :"",
        "symbol"                        :"",
        "transitive verb"               : "vt.",
        "v."                            : "v.",
        "verb"                          : "v.",
        "vi."                           : "vi.",
        "vt."                           : "vt.",
    }
    dump_pos_list = False

    for word, defi in j.items():
        definations = defi.get("definations")
        forms       = defi.get("forms") or dict()

        # 去掉这些字符
        if re.search("^[a-z]+$", word) is None or definations is None:
            continue

        pos_str = ""
        pos_list = []
        for defi in definations:
            defination = defi.get("defination") or ""
            p1   = defi.get("pos") or []

            if isinstance(p1, list):
                pos_list += p1
            else:
                pos_list += [p1]
        pos_list = list(set(pos_list))
        if dump_pos_list is False:
            fl = []
            for p in pos_list:
                if len(pos_m[p.strip().lower()]):
                    fl.append(pos_m[p.strip().lower()])
            pos_str = "#".join(p.strip().lower() for p in fl)
        else:
            for p in pos_list: 
                if pos_m.get(p.strip().lower()) is None:
                    fo.write(p.strip().lower()+"\n")

        form_str = ""
        if forms:
            comparative             =forms.get("comparative")         or []
            past_participle         =forms.get("past participle")     or []
            past_tense              =forms.get("past tense")          or []
            plural                  =forms.get("plural")              or []
            present_participle      =forms.get("present participle")  or []
            superlative             =forms.get("superlative")         or []
            third_person_singular=forms.get("third person singular")  or []
            singular                =forms.get("singular")  or []



            form_str = "{}|{}|{}|{}|{}|{}|{}|{}".format(",".join(p for p in plural), ",".join(p for p in comparative), ",".join(p for p in superlative), ",".join(p for p in third_person_singular), ",".join(p for p in present_participle), ",".join(p for p in past_participle), ",".join(p for p in past_tense), ",".join(p for p in singular) )

        if len(pos_str) > 0:
            #fo.write(word+"|"+pos_str+"\n")
            if dump_pos_list is False:
                fo.write(word+"|"+pos_str+"|"+form_str+"|"+"\n")
            #fo.write(pos_str+"\n")
            #fo.write(form_str+"\n")
            pass



    fo.close()

#=============================
main()